package com.ch.dream.base;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.ch.dream.R;
import com.ch.dream.common.CommonData;
import com.ch.dream.tools.ViewTools;
import com.gfan.sdk.statitistics.GFAgent;

public abstract class BaseActivity extends Activity implements OnClickListener{
	
	protected String BACK="返回";
	
	public static final int RIGHT_BUTTON = 0;
	public static final int BG_BUTTON1 = 2;
	public static final int BG_BUTTON2 = 3;
	public static final int LEFT_BUTTON = 1;
	public RelativeLayout baseLayout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
		LayoutParams layoutParams =new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		baseLayout = new RelativeLayout(this);
		baseLayout.setLayoutParams(layoutParams);
		CommonData.currentActivity = this;
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		GFAgent.onResume(this);
		CommonData.currentActivity = this;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1, 1, "关于");
		menu.add(0, 2, 2, "退出");
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		GFAgent.onPause(this);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == 1){
			AssetManager assetManager = getAssets();
			StringBuilder sb =new StringBuilder();
			try{
				InputStream input = assetManager.open("about.txt");
				BufferedReader br =new BufferedReader(new InputStreamReader(input,"utf-8"));
				String line;
				while((line = br.readLine()) != null){
					sb.append(line+"\n");
				}
			}catch (Exception e) {
				e.printStackTrace();
			}
			
			ViewTools.showMessage(CommonData.currentActivity,"关于故事梦", sb.toString());
		}else if(item.getItemId() == 2){
			int id = android.os.Process.myPid();
			android.os.Process.killProcess(id);
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onDestroy() {
		if(ViewTools.alertDialog!=null && ViewTools.alertDialog.isShowing()){
			ViewTools.alertDialog.dismiss();
		}
		super.onDestroy();
	}
	// 处理标题栏事件:
	protected abstract void handlerTitleBarEvent(int mode);
		
	public void setTitle(String left,String middle,String right){
		baseLayout.addView(addTitle(this, left, middle, right));
	}
	
	/**
	 * 获取一个头部
	 * @param context
	 * @param left
	 * @param middle
	 * @param right
	 * @return
	 */
	public View addTitle(Context context,String left,String middle,String right){
		LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View titleView = mInflater.inflate(R.layout.title, null);
		TextView leftView = (TextView)titleView.findViewById(R.id.titleLeft);
		TextView middleView = (TextView)titleView.findViewById(R.id.titleMiddle);
		TextView rightView = (TextView)titleView.findViewById(R.id.titleRight);
		if(left.equals("false")){
			leftView.setVisibility(View.GONE);
		}else{
			leftView.setText(left);
			leftView.setOnClickListener(this);
		}
		if(middle.equals("false")){
			middleView.setVisibility(View.GONE);
		}else{
			middleView.setOnClickListener(this);
			middleView.setText(middle);
		}
		if(right.equals("false")){
			rightView.setVisibility(View.GONE);
		}else{
			rightView.setOnClickListener(this);
			rightView.setText(right);
		}
		return titleView;
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.titleLeft:
			handlerTitleBarEvent(LEFT_BUTTON);
			break;
		case R.id.titleRight:
			handlerTitleBarEvent(RIGHT_BUTTON);
			break;
		case R.id.titleMiddle:
			handlerTitleBarEvent(BG_BUTTON1);
			break;
		}
	}

}
